//
//  PowerHistoryListVC.swift
//  LeGou
//
//  Created by 刘亿前 on 2022/9/14.
//

import UIKit

class PowerHistoryListVC: ZMViewController {
    
    // MARK: - UI
    lazy var topTip: ZMLabel = {
        let lab = ZMLabel(frame: CGRect(x: 0, y: 0, width: SCREEN_W, height: 44))
        lab.backgroundColor = "#FFE9E9".zm.hex()
        lab.text = "预估返利金额仅供参考，具体的返利金额要看实际充值金额"
        lab.textColor = "#DE3F26".zm.hex()
        lab.font = UIFont.zm.fontMedium(size: 12)
        lab.zm_edgeInsets = UIEdgeInsets(top: 0, left: 32, bottom: 0, right: 0)
        return lab
    }()
    lazy var listTable: UITableView = {
        let table = UITableView(frame: SCREEN_BOUNDS, style: .plain)
        table.delegate = self
        table.dataSource = self
        table.separatorStyle = .none
        table.register(UINib(nibName: "PowerHistoryListCell", bundle: nil), forCellReuseIdentifier: "PowerHistoryListCell")
        table.showsVerticalScrollIndicator = false
        table.tableFooterView = UIView()
        table.backgroundColor = "#F5F5F5".zm.hex()
        table.tableHeaderView = topTip
        view.addSubview(table)
//        table.snp.makeConstraints { make in
////            make.top.equalTo(self.topTip.snp.bottom)
////            make.left.right.bottom.equalToSuperview()
//            make.edges.equalToSuperview()
//        }
        addRefresh(scrollView: table) { [unowned self] refresh in
            self.page = refresh ?  1 : self.page + 1
            self.requestList()
        }
        table.mj_footer?.isHidden = true
        return table
    }()
    
    // MARK: - 属性
    var page = 1
    let pageSize = 10
    var dataArr = [PowerHistoryModel]()
    var state = "0"

    override func viewDidLoad() {
        super.viewDidLoad()
        glt_scrollView = listTable
        listTable.mj_header?.beginRefreshing()
        emptyImg = "nodata"
        emptyTitle = "暂无缴费"
    }
}

extension PowerHistoryListVC: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArr.count
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PowerHistoryListCell", for: indexPath) as! PowerHistoryListCell
        cell.setUI(dataArr[indexPath.row])
        return cell
    }
}

// MARK: - Request Action
extension PowerHistoryListVC {
    func requestList() {
        let pa = ["page":page,"limit":pageSize,"type":state] as [String: Any]
        ZMUtil.listRequest(type: PowerHistoryModel.self, api: PowerAPI.power_order_list(pa)) { resp in
            if resp.code == .success {
                self.relodaData(resp.data ?? [])
            } else {
                self.relodaData()
            }
        }
    }
    
    func relodaData(_ data: [PowerHistoryModel] = []) {
        addNodataScrollView(scrollView: listTable)
        listTable.mj_header?.endRefreshing()
        listTable.mj_footer?.endRefreshing()
        if page == 1 {
            dataArr.removeAll()
        }
        if data.count < pageSize {
            listTable.mj_footer?.endRefreshingWithNoMoreData()
        }
        dataArr.append(contentsOf: data)
        listTable.mj_footer?.isHidden = dataArr.count == 0
        listTable.reloadData()
    }
}
